<template>
  <div class="main">
    <div class="login_box">
      <h3>就诊预约平台</h3>
      <van-field
        v-model="loginForm.username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="rules.username"
      />
      <van-field
        v-model="loginForm.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          @click="loginConfirm"
          >登录</van-button
        >
      </div>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="primary"
          native-type="submit"
          @click="registerConfirm"
          >注册</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: '',
        passport: ''
      },
      rules: {
        username: [{ required: true, message: '请填写用户名' }]
      }
    }
  },
  methods: {
    async loginConfirm () {
      const { data: res } = await this.$http.post('/user/patient/login', this.loginForm)
      if (res.status === 200) {
        window.sessionStorage.setItem('token', res.token)
        window.sessionStorage.setItem('id', res.patient._id)
        this.$router.push('/index')
        this.$Toast.success('登陆成功')
      } else if(res.status === 203) {
        return this.$Toast.fail('账号或密码错误')
      }else{
        return this.$Toast.fail('账号已冻结')
      }
    },
    async registerConfirm () {
      const { data: res } = await this.$http.post('/user/patient/register', this.loginForm)
      if (res.status !== 200) {
        return this.$Toast.fail('用户名已存在')
      }
      this.$Toast.success('注册成功')
    }
  }
}
</script>

<style scoped>
.main {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url(./assets/img/bg.jpg);
}
.login_box {
  width: 90%;
  box-sizing: border-box;
  background-color: #fff;
  opacity: .8;
  border-radius: 9px;
}
.login_box h3 {
  text-align: center;
}
</style>
